/**
 * 
 */
package de.cbf.cam.dsa.battle.ui.composites;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import de.cbf.cam.dsa.battle.ui.listeners.EnterNumbers;
import de.cbf.cam.dsa.model.util.Position3f;

/**
 * @author Nebelritter
 *
 */
public class CompositePositionEdit extends Composite {
	
	public static final int X = 1;
	public static final int Y = 2;
	public static final int Z = 3;

	/**
	 * @uml.property  name="textPosX"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Text textPosX;
	/**
	 * @uml.property  name="textPosY"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Text textPosY;
	/**
	 * @uml.property  name="textPosZ"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Text textPosZ;	
	
	/**
	 * @uml.property  name="positionModifyAdvancer"
	 * @uml.associationEnd  multiplicity="(1 1)" inverse="this$0:de.cbf.cam.dsa.battle.ui.composites.CompositePositionEdit$PositionModifiedAdvancer"
	 */
	private PositionModifiedAdvancer positionModifyAdvancer = new PositionModifiedAdvancer();

	/**
	 * @uml.property  name="numberVerifyier"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private EnterNumbers numberVerifyier;
	
	public CompositePositionEdit(Composite parent, int style) {
		super(parent, style);
		
		String newCheckString	= EnterNumbers.getInstance().getCheckString()+".";
		numberVerifyier			= EnterNumbers.getInstance(newCheckString);
		
		GridLayout layout = new GridLayout(6,false);
		layout.marginHeight = 0;
		layout.marginWidth  = 0;
		setLayout(layout);
		
		GridData posXYZLayoutData = new GridData(SWT.CENTER,SWT.CENTER,false,false,1,1);
		posXYZLayoutData.widthHint = 30;
		
		Label labelStaticPosition;
		labelStaticPosition = new Label(this, SWT.LEFT);
		labelStaticPosition.setLayoutData(new GridData(SWT.CENTER,SWT.CENTER,false,false,1,1));
		labelStaticPosition.setText("X:");
		
		textPosX = new Text(this, SWT.BORDER);
		textPosX.setLayoutData(posXYZLayoutData);
		textPosX.setText("");
		textPosX.addVerifyListener(numberVerifyier);
		textPosX.addKeyListener(positionModifyAdvancer);
		
		labelStaticPosition = new Label(this, SWT.LEFT);
		labelStaticPosition.setLayoutData(new GridData(SWT.CENTER,SWT.CENTER,false,false,1,1));
		labelStaticPosition.setText("Y:");
		
		textPosY = new Text(this, SWT.BORDER);
		textPosY.setLayoutData(posXYZLayoutData);
		textPosY.setText("");
		textPosY.addVerifyListener(numberVerifyier);
		textPosY.addKeyListener(positionModifyAdvancer);
		
		labelStaticPosition = new Label(this, SWT.LEFT);
		labelStaticPosition.setLayoutData(new GridData(SWT.CENTER,SWT.CENTER,false,false,1,1));
		labelStaticPosition.setText("Z:");
		
		textPosZ = new Text(this, SWT.BORDER);
		textPosZ.setLayoutData(posXYZLayoutData);
		textPosZ.setText("");
		textPosZ.addVerifyListener(numberVerifyier);
		textPosZ.addKeyListener(positionModifyAdvancer);
	}
	
	public void setEnabled(boolean enabled){
		textPosX.setEnabled(enabled);
		textPosY.setEnabled(enabled);
		textPosZ.setEnabled(enabled);
	}

	public void clear(){
		textPosX.setText("");
		textPosY.setText("");
		textPosZ.setText("");
	}
	
	
	public void setPosition(Position3f position){
		textPosX.setText(position.x+"");
		textPosY.setText(position.y+"");
		textPosZ.setText(position.z+"");
	}
	public Position3f getPosition(){
		Position3f position = new Position3f();
		try{
			String textX = textPosX.getText();
			position.x = Float.parseFloat(textX);
			
			String textY = textPosY.getText();
			position.y = Float.parseFloat(textY);
			
			String textZ = textPosZ.getText();
			position.z = Float.parseFloat(textZ);
			
		}catch (NumberFormatException e) {
			return null;
		}
		return position;		
	}
	
	public Position3f changePosition(int axis,float value){		
		try {
			switch (axis) {
				case X:{
					float newValue = Float.parseFloat(textPosX.getText())+value;
					textPosX.setText(newValue+"");
					break;
				}
				case Y:{
					float newValue = Float.parseFloat(textPosY.getText())+value;
					textPosY.setText(newValue+"");				
					break;
				}
				case Z:{
					float newValue = Float.parseFloat(textPosZ.getText())+value;
					textPosZ.setText(newValue+"");
					break;				
				}		
			}
		} catch (NumberFormatException e) {
			//do nothing
		}
		return getPosition();
	}
	
	/**
	 * @uml.property  name="positionModifiedListeners"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="java.awt.event.ActionListener"
	 */
	private List<ActionListener> positionModifiedListeners = new ArrayList<ActionListener>();
	public boolean registerPositionModifiedListener(ActionListener listener){
		return positionModifiedListeners.add(listener);
	}
	public boolean deRegisterpositionModifiedListener(ActionListener listener){
		return positionModifiedListeners.remove(listener);
	}	
	private class PositionModifiedAdvancer extends KeyAdapter{
		@Override
		public void keyReleased(KeyEvent e) {
			ActionEvent event = new ActionEvent(CompositePositionEdit.this, ActionEvent.ACTION_PERFORMED, "positionModified");
			for (ActionListener listener : positionModifiedListeners) {
				listener.actionPerformed(event);
			}	
		}		
	}
}
